﻿using UnityEngine;
using System.Collections;

public class StickMan : MonoBehaviour
{
    private bool isRight;

    [SerializeField]
    private float leftX;
    [SerializeField]
    private float rightX;
    private Vector3 destinationPosition;
    private Animator anim;


    private GameController_Stream gameStream;
    void Awake() 
    {
        gameStream = FindObjectOfType<GameController_Stream>();
        anim = GetComponent<Animator>();
        destinationPosition = transform.position;
        isRight = false;
    }
    void OnCollisionEnter2D() 
    {
        
        gameStream.GameOver();
        Time.timeScale = 0;
    }

    void Update() 
    {
        transform.position = Vector3.Lerp(transform.position, destinationPosition, Time.deltaTime*20);
    }

    public void Flip() 
    {
        audio.PlayOneShot(audio.clip);
        anim.SetTrigger("Jump");
        isRight = !isRight;
        transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
        if (isRight)
        {
            destinationPosition = new Vector3(rightX, transform.position.y, transform.position.z);
        }
        else 
        {
            destinationPosition = new Vector3(leftX, transform.position.y, transform.position.z);
        }
    }
}
